﻿using System.Collections.Generic;
using System.ComponentModel;

namespace Borg.Framework
{
    public class PluggedPlugsDataModel : IDataModel<PlugInfo>
    {
        public event ListChangedEventHandler ListChanged;

        public PluggedPlugsDataModel(SlotInfo slot)
        {
            Slot = slot;
            
            Slot.Plugged += Plugged;
            Slot.Unplugged += Unplugged;
        }

        public IEnumerator<PlugInfo> GetEnumerator()
        {
            return Slot.PluggedPlugInfos.GetEnumerator();
        }

        private void InvokeListChanged()
        {
            if (ListChanged == null)
                return;

            var e = new ListChangedEventArgs(ListChangedType.Reset, -1);
            ListChanged(this, e);
        }

        private void Plugged(object sender, PlugEventArgs args)
        {
            InvokeListChanged();
        }

        private void Unplugged(object sender, PlugEventArgs args)
        {
            InvokeListChanged();
        }

        public int Count
        {
            get { return Slot.PluggedPlugInfos.Count; }
        }

        public PlugInfo this[int index]
        {
            get { return Slot.PluggedPlugInfos[index]; }
        }

        private SlotInfo Slot { get; set; }
    }
}

